package eu.testing.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import eu.testing.system.dao.UserDataDao;
import eu.testing.system.models.UserData;

public class UserDataDaoImpl extends SimpleJdbcDaoSupport implements
		UserDataDao {
	private final String SQL_INSERT = "INSERT INTO users (USERNAME, PASSWORD, AUTHORITY, GROUP_NAME, ENABLED, FIRST_NAME, LAST_NAME, EMAIL) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
	private final String SQL_SELECT = "SELECT * FROM users WHERE USERNAME = ?";
	private final String SQL_SELECT_BY_USERNAME = "SELECT COUNT(*) FROM users WHERE USERNAME = ?";
	private final String SQL_DELETE = "DELETE FROM users WHERE USERNAME = ?";
	private final String SQL_UPDATE = "UPDATE users SET PASSWORD = ?, AUTHORITY = ?,GROUP_NAME = ?, FIRST_NAME = ?, LAST_NAME= ?, EMAIL = ? WHERE USERNAME = ?";
	private static final String SQL_SELECT_ALL = "SELECT * FROM users";
 
	@Override
	public void insertUser(UserData user) {
		if (!ifUserAlreadtExists(user.getUsername())) {
			getJdbcTemplate().update(SQL_INSERT, user.getUsername(),
					user.getPassword(), getAuthority(user.getGroup()),
					user.getGroup(), 1, user.getFirstName(),
					user.getLastName(), user.getEmail());
		} else {
			throw new NullPointerException();
		}
	}

	@Override
	public UserData getUser(String username) {
		UserData user = getSimpleJdbcTemplate().queryForObject(SQL_SELECT,
				new UserDataParameterizedRowMapper(), username);
		return user;

	}

	@Override
	public void deleteUser(String username) {
		getJdbcTemplate().update(SQL_DELETE, username);

	}

	@Override
	public void editUser(UserData user) {
		getJdbcTemplate().update(SQL_UPDATE, user.getPassword(),
				getAuthority(user.getGroup()),user.getGroup(), user.getFirstName(), user.getLastName(),
				user.getEmail(), user.getUsername());

	}

	@Override
	public List<UserData> getAllUsers() {
		List<UserData> users = getSimpleJdbcTemplate().query(SQL_SELECT_ALL,
				new UserDataParameterizedRowMapper());
		return users;
	}

	private boolean ifUserAlreadtExists(String username) {
		int i = getSimpleJdbcTemplate().queryForInt(SQL_SELECT_BY_USERNAME,
				username);
		if (i == 0) {
			return false;
		}

		return true;
	}

	private String getAuthority(String group) {
		String authority;
		if (group.equals("old!")) {
			authority = "ROLE_ADMIN";
		} else {
			authority = "ROLE_USER";
		}
		return authority;
	}

	public class UserDataParameterizedRowMapper implements
			ParameterizedRowMapper<UserData> {
		@Override
		public UserData mapRow(ResultSet rs, int rowNum) throws SQLException {
			UserData ud = new UserData();
			ud.setUsername(rs.getString("USERNAME"));
			ud.setEmail(rs.getString("EMAIL"));
			ud.setAuthority(rs.getString("AUTHORITY"));
			ud.setGroup(rs.getString("GROUP_NAME"));
			ud.setEnabled(rs.getBoolean("ENABLED"));
			ud.setFirstName(rs.getString("FIRST_NAME"));
			ud.setLastName(rs.getString("LAST_NAME"));
			ud.setPassword(rs.getString("PASSWORD"));
			return ud;
		}

	}

}
